﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="VehicleEditorViewModel.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the VehicleEditorViewModel type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Windows.Client.ViewModel
{
    using System;
    using System.Diagnostics.Contracts;
    using System.Windows;
    using Business.Interfaces;
    using Hisaab.Common;
    using Hisaab.Windows.Common;

    using Microsoft.Practices.ServiceLocation;

    using Model;

    /// <summary>
    /// Defines the <seealso cref="VehicleEditorViewModel"/> type.
    /// </summary>
    public class VehicleEditorViewModel : DialogViewModel
    {
        /// <summary>
        /// Vehicle manaager
        /// </summary>
        private readonly IVehicleManager _vehicleManager;

        /// <summary>
        /// The session manager
        /// </summary>
        private readonly ISession _session;

        /// <summary>
        /// Selected vehicle
        /// </summary>
        private Vehicle _selectedVehicle;

        /// <summary>
        /// Initializes a new instance of the <see cref="VehicleEditorViewModel"/> class.
        /// </summary>
        /// <param name="vehicle">The vehicle.</param>
        public VehicleEditorViewModel(Vehicle vehicle)
            : this(
            vehicle,
            ServiceLocator.Current.GetInstance<IVehicleManager>(),
            ServiceLocator.Current.GetInstance<ISession>())
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="VehicleEditorViewModel"/> class.
        /// </summary>
        /// <param name="vehicle">The vehicle.</param>
        /// <param name="vehicleManager">The vehicle manager.</param>
        /// <param name="session">The session</param>
        public VehicleEditorViewModel(Vehicle vehicle, IVehicleManager vehicleManager, ISession session)
        {
            Contract.Requires<ArgumentNullException>(vehicleManager != null);
            Contract.Requires<ArgumentNullException>(session != null);

            _vehicleManager = vehicleManager;
            _session = session;
            _selectedVehicle = vehicle ?? new Vehicle();
            IsNewVehicle = vehicle == null;
            ResizeMode = ResizeMode.NoResize;
        }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is new vehicle.
        /// </summary>
        /// <value>
        /// True if this instance is new vehicle; otherwise, false.
        /// </value>
        public bool IsNewVehicle { get; set; }

        /// <summary>
        /// Gets or sets the selected vehicle.
        /// </summary>
        /// <value>
        /// The selected vehicle.
        /// </value>
        public Vehicle SelectedVehicle
        {
            get
            {
                return _selectedVehicle;
            }

            set
            {
                if (_selectedVehicle != value)
                {
                    _selectedVehicle = value;
                    OnPropertyChanged("SelectedVehicle");
                }
            }
        }

        /// <summary>
        /// Called when [ok pressed].
        /// </summary>
        /// <returns>
        /// True if dialog to be closed, false to avoid closing
        /// </returns>
        public override bool? OnOkDialog()
        {
            if (IsNewVehicle)
            {
                SelectedVehicle.UserId = _session.LoggedUser;
                _vehicleManager.Add(SelectedVehicle.Map<Vehicle, Business.Entities.Vehicle>());
            }
            else
            {
                _vehicleManager.Update(SelectedVehicle.Map<Vehicle, Business.Entities.Vehicle>());
            }

            return true;
        }
    }
}